package com.ruicar.afs.cloud.afscase.approvetask.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.approvetask.entity.WorkProcessScheduleInfoTemp;
import com.ruicar.afs.cloud.afscase.approvetask.mapper.WorkProcessScheduleInfoTempMapper;
import com.ruicar.afs.cloud.afscase.approvetask.service.WorkProcessScheduleInfoTempService;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.processor.util.WorkflowTypeUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-05-17 11:34
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class WorkProcessScheduleInfoTempServiceImpl extends ServiceImpl<WorkProcessScheduleInfoTempMapper, WorkProcessScheduleInfoTemp> implements WorkProcessScheduleInfoTempService {

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean saveWithNewTransactional(WorkProcessScheduleInfoTemp temp){
        return this.save(temp);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean removeWithNewTransactional(WorkProcessScheduleInfoTemp temp) {
        return Objects.isNull(temp) || Objects.isNull(temp.getId()) ? false : baseMapper.removeById(temp.getId());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public WorkProcessScheduleInfoTemp createWorkProcessor(CaseContractInfo contractInfo, String afsFlowKey) {
        // 保存工作流实例
        WorkProcessScheduleInfoTemp info = new WorkProcessScheduleInfoTemp();
        info.setAfsFlowKey(afsFlowKey);
        info.setApplyNo(contractInfo.getApplyNo());
        info.setContractNo(contractInfo.getContractNo());
        info.setProcessName(WorkflowTypeUtil.getApproveTypeName(afsFlowKey));
        info.setStartTime(new Date());
        this.save(info);
        return info;
    }
}
